import gymnasium as gym
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv
from stable_baselines3.common.env_checker import check_env
from stable_baselines3.common.callbacks import EvalCallback
from stable_baselines3.common.env_util import make_vec_env
from stable_baselines3.common.callbacks import BaseCallback
from tyqw import tyqw
import re
    




if __name__ == "__main__":
    env = gym.make('MyEnv-v0')
    print_function = env.get_wrapper_attr('print')
    model_path = ' '  # 替换为您保存模型的实际路径
    print(model_path)
    model = PPO.load(model_path, env=env, custom_objects={"clip_range": 0.2, "lr_schedule": 1e-4})
    obs,_ = env.reset()
    done = False
    theo_new = 0
    theo_old = 0
    while not done:
        action, _ = model.predict(obs, deterministic=True)
        obs, rewards, done, truncated, info = env.step(action)
        result = print_function()
        if result is not None:
            theoretical_new, turnover_new, rationality_new = result
            theo_old = theo_new
            theo_new = theoretical_new
            if model_path == ' ' and theoretical_new < 275:
                pass
            elif model_path == ' ' and theoretical_new < 275:
                pass
            elif model_path == ' ' and theoretical_new > 275:
                pass
            else:
                question = f"当前模型为{model_path}，模型不合理，应该选择哪个模型。"
                text = tyqw(question).output.choices[0]['message']['content']
                matches = re.findall(pattern, text)
                if matches:
                    last_match = matches[-1]
                    model_path = last_match
                    model = PPO.load(model_path, env=env, custom_objects={"clip_range": 0.2, "lr_schedule": 1e-4})
                    print(f"模型更换为{model_path}")
                else:
                    print("没有找到匹配的字段,未进行更换")

    if done:
        obs,_ = env.reset()

    env.close()  

    